package com.asset.dispoal.address.service.commom;

import com.asset.dispoal.address.domain.db.entity.Area;
import com.asset.dispoal.address.domain.db.mapper.AreaMapper;
import com.asset.dispoal.address.dto.AreaRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.id.BaseIdeable;
import tech.mhuang.pacebox.springboot.core.spring.util.DataUtil;
import tech.mhuang.pacebox.springboot.protocol.Result;

@Component
public class AreaInsertCommand implements BaseCommand<AreaRequest, Result> {

    @Autowired
    private AreaMapper areaMapper;

    @Autowired
    private BaseIdeable<String> ideable;

    @Override
    public Result execute(AreaRequest area) {
        Area check = areaMapper.check(area.getOldName(), area.getCity());
        if (check != null) {
            return Result.faild("当前城市已经有检测项了");
        }
        area.setId(ideable.generateId());
        Area updateDo = DataUtil.copyTo(area, Area.class);
        areaMapper.insert(updateDo);
        return Result.ok();
    }
}
